package com.suxin.netty.remoting;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.nio.channels.Channel;

/**
 * @author Tang
 * @classname ResponseFuture
 * @description [ 信息响应 ]
 * @date 2022/12/11 15:52
 */
@Slf4j
@Data
public class ResponseFuture {

    /**
     * 对应请求的标志
     */
    private String opaque;

    /**
     * 响应完成的回调函数
     */
    private InvokeCallback invokeCallback;

    /**
     * 超时时间
     */
    private final long timeoutMillis;

    /**
     * 初始时间
     */
    private final long beginTimeStamp = System.currentTimeMillis();

    /**
     * 连接
     */
    private final Channel channel;

    public ResponseFuture(String opaque, InvokeCallback invokeCallback, long timeoutMillis, Channel channel) {
        this.opaque = opaque;
        this.invokeCallback = invokeCallback;
        this.timeoutMillis = timeoutMillis;
        this.channel = channel;
    }

    /**
     * 执行 回调
     */
    public void executeInvokeCallback() {
        if (this.invokeCallback != null) {
            this.invokeCallback.operationComplete(this);
        }
    }

    public boolean isTimeout() {
        long diff = System.currentTimeMillis() - this.beginTimeStamp;
        return diff > timeoutMillis;
    }

}
